package com.my.service.impl;

import com.my.mapper.CheckLogMapper;
import com.my.pojo.Check;
import com.my.pojo.CheckLog;
import com.my.service.ICheckLogService;
import lombok.Data;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;

@Service
@Data
public class CheckLogService implements ICheckLogService , AfterReturningAdvice {
    private CheckLogMapper checkLogMapper;
    private HttpServletRequest request;
    public HttpServletRequest getRequest(){
        return request;
    }
    public void setRequest(HttpServletRequest request){
        this.request = request;
    }

    @Override
    public void addFindCheckLog() {
        HttpSession session = request.getSession();
        String checkTime = (String)session.getAttribute("checkTime");
        Object userId = session.getAttribute("userId");
        Integer uId =1;
        if(null != userId){
            uId = (Integer)userId;
        }
//        Integer userId = (Integer)session.getAttribute("userId");
        String ip =(String)session.getAttribute("ip");
        CheckLog checkLog = new CheckLog();
        checkLog.setCheckTime(checkTime);
        checkLog.setCheckIp(ip);
        checkLog.setUserId(uId);
        this.addCheckLog(checkLog);
    }

    @Override
    public void addCheckLog(CheckLog checkLog) {
        checkLogMapper.addCheckLog(checkLog);
    }
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        HttpSession session = request.getSession();
        Check check = (Check) session.getAttribute("check");
        CheckLog checkLog = new CheckLog();
        checkLog.setUserId(check.getUserId());
        checkLog.setCheckTime(check.getCheckTime());
        checkLog.setCheckTag(check.getCheckTag());
        checkLog.setCheckIp((String) session.getAttribute("ip"));
        this.addCheckLog(checkLog);
    }
}
